//
//  GHSAppealDetailViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/22.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSAppealDetailViewController.h"
#import "GHSOD_SectionHeaderView.h"
#import "GHSOD_GoodsTableViewCell.h"
#import "GHSOD_BaseTableViewCell.h"
#import "GHSAD_OrderInfoTableViewCell.h"
#import "GHSAD_UserInfoTableViewCell.h"
#import "GHSAD_HHInfoTableViewCell.h"
#import "GHSAD_descTableViewCell.h"
#import "GHSAD_WattingTableViewCell.h"
#import "GHSAD_SuccessTableViewCell.h"
#import "GHSAD_FailTableViewCell.h"


@interface GHSAppealDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray<SPSection *> *sections;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) GHSAD_HHInfoTableViewCell *hhInfoCell;
@property (nonatomic,strong) GHSAD_descTableViewCell *desc1Cell;
@property (nonatomic,strong) GHSAD_descTableViewCell *desc2Cell;
@property (nonatomic,strong) GHSAD_SuccessTableViewCell *sCell;
@property (nonatomic,assign) GHSAppealStatus status;

@end

@implementation GHSAppealDetailViewController

- (instancetype)initWithAppealStatus:(GHSAppealStatus)status
{
    if(self = [super init]){
        _status = status;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"申诉详情";
    [self initDataSource];
    [self setupTableView];
}



- (void)initDataSource
{
    NSArray *items;
    items = @[@"商品信息",@"订单信息",@"买家信息",@"换货信息",@"供应商处理结果",@"申诉详情",@"申诉结果"];
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SPSection *sec = [SPSection new];
        sec.title = obj;
        sec.open = YES;
        [self.sections addObject:sec];
    }];
}




- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView =tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_SectionHeaderView class]) bundle:nil]  forHeaderFooterViewReuseIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_GoodsTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_OrderInfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_OrderInfoTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_UserInfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_UserInfoTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_HHInfoTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_HHInfoTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_descTableViewCell class]) bundle:nil]  forCellReuseIdentifier:@"descCell1"];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_descTableViewCell class]) bundle:nil]  forCellReuseIdentifier:@"descCell2"];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_WattingTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_WattingTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_SuccessTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_SuccessTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSAD_FailTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSAD_FailTableViewCell class])];
    
}


#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SPSection *sec = self.sections[section];
    return sec.isOpen?1:0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    switch (indexPath.section) {
        case 0://商品信息
            height = 160;
            break;
        case 1://订单信息
            height = 205;
            break;
        case 2://买家信息
            height = 156;
            break;
        case 3://换货信息
            height = self.hhInfoCell.cellH;
            break;
        case 4://供应商处理结果
            height = self.desc1Cell.cellH;
            break;
        case 5://申诉详情
            height = self.desc2Cell.cellH;
            break;
        case 6://申诉结果
            if(_status == GHSAppealStatus_Processing){
                height = 205;
            }else if (_status == GHSAppealStatus_Success){
                height = self.sCell.cellH;
                RZLog(@"--height=%f",height);
            }else{
                height = 185;
            }
            break;
    }
    return height;
}




- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPSection *sec = _sections[section];
    GHSOD_SectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    headerV.title.text = sec.title;
    headerV.arrow.highlighted = sec.open;
    headerV.arrow.hidden = NO;
    headerV.arrow.hidden = (section==0 || section == 5 || section == 6);
    __weak typeof(headerV) weakO = headerV;
    headerV.onClickBlock = ^{
        if(weakO.arrow.hidden) return;
        sec.open = !sec.isOpen;
        weakO.arrow.highlighted = sec.open;
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    return headerV;
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPSection *sec = self.sections[indexPath.section];
    UITableViewCell *cell;
    switch (indexPath.section) {
        case 0:{//商品信息
            GHSOD_GoodsTableViewCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
            cell = goodsCell;
            break;
        }
            
        case 1:{//订单信息
            GHSAD_OrderInfoTableViewCell *orderInfoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_OrderInfoTableViewCell class])];
            orderInfoCell.onClickBlock = ^{
                RZLog(@"---copy---");
            };
            orderInfoCell.sn.text = @"2226855021594056785";
            orderInfoCell.time_pay.text = @"2018-10-05  21:40:25";
            orderInfoCell.time_send.text = @"2018-10-05  21:40:25";
            orderInfoCell.time_recive.text = @"2018-10-05  21:40:25";
            cell = orderInfoCell;
            break;
        }
            
        case 2:{//买家信息
            GHSAD_UserInfoTableViewCell *userInfoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_UserInfoTableViewCell class])];
            userInfoCell.onClickBlock = ^{
                RZLog(@"---contact---");
            };
            userInfoCell.name.text = @"高灵芝";
            userInfoCell.tel.text = @"18696164227";
            userInfoCell.address.text = @"昆明市官渡区和平新村502";
            cell = userInfoCell;
            break;
        }
            
        case 3:{//换货信息
            cell = self.hhInfoCell;
            break;
        }
            
        case 4:{//供应商处理结果
            self.desc1Cell.desc2.text = @"驳回换货申请";
            self.desc1Cell.desc3Str = @"不好意思，我们查看过您上传的相关图片，没有质量问题，所以决定驳回您的换货申请，谢谢您的支持。";
            cell = self.desc1Cell;
            break;
        }
        case 5:{//申诉详情
            self.desc2Cell.desc2.text = @"对商家处理结果不满意";
            self.desc2Cell.desc3Str = @"对商家处理结果不满意对商家处理结果不满意对商家处理结果不满意对商家处理结果不满意对商家处理结果不满意";
            cell = self.desc2Cell;
            break;
        }
        case 6:{//申诉结果
            if(_status == GHSAppealStatus_Processing){
                GHSAD_WattingTableViewCell *wCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_WattingTableViewCell class])];
                cell = wCell;
            }else if (_status == GHSAppealStatus_Success){
                cell = self.sCell;
                
            }else{
                GHSAD_FailTableViewCell *fCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_FailTableViewCell class])];
                cell = fCell;
            }
            break;
        }
    }
    return cell;
}



#pragma mark--bottomBtnOnClick
- (void)bottomBtnOnClick
{
    RZLog(@"bottomBtnOnClick");
}



#pragma mark--setter&&getter

- (NSMutableArray<SPSection *> *)sections
{
    if(!_sections){
        _sections = [NSMutableArray array];
    }
    return _sections;
}


- (GHSAD_HHInfoTableViewCell *)hhInfoCell
{
    if(!_hhInfoCell){
        _hhInfoCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_HHInfoTableViewCell class])];
    }
    return _hhInfoCell;
}

- (GHSAD_descTableViewCell *)desc1Cell
{
    if(!_desc1Cell){
        _desc1Cell = [self.tableView dequeueReusableCellWithIdentifier:@"descCell1"];
        _desc1Cell.title1.text = @"处理时间";
        _desc1Cell.title2.text = @"处理结果";
        _desc1Cell.title3.text = @"原因说明";
        _desc1Cell.desc3TextColor = RZ_COLOR_ALPHA(51, 51, 51, 0.5);
    }
    return _desc1Cell;
}


- (GHSAD_descTableViewCell *)desc2Cell
{
    if(!_desc2Cell){
        _desc2Cell = [self.tableView dequeueReusableCellWithIdentifier:@"descCell2"];
        _desc2Cell.title1.text = @"申诉时间";
        _desc2Cell.title2.text = @"申诉原因";
        _desc2Cell.title3.text = @"详细说明";
        _desc2Cell.desc3TextColor = RZ_COLOR(51, 51, 51);
    }
    return _desc2Cell;
}


- (GHSAD_SuccessTableViewCell *)sCell
{
    if(!_sCell){
        _sCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSAD_SuccessTableViewCell class])];
        [_sCell setupData];
    }
    return _sCell;
}

@end
